import 'package:get/get.dart';
import 'package:xmshop/app/models/user_model.dart';
import '../../../services/userService.dart';

class UserController extends GetxController {
  var userInfo = UserModel().obs;
  var isLogin = false.obs;

  @override
  void onInit() {
    super.onInit();
    getUserInfo();
  }

  getUserInfo() async {
    isLogin.value = await UserService.getUserLoginState();
    if (isLogin.value) {
      var userInfoMap = await UserService.getUserInfo();
      if (userInfoMap != null) {
        userInfo.value =
            UserModel.fromJson(userInfoMap as Map<String, dynamic>);
        update();
      }
    }
  }

  logout() async {
    isLogin.value = false;
    UserService.clearUserInfo();
    // 把userInfo设置为空
    userInfo.value = UserModel();
    update();
  }
}
